import {isArr} from "@/utils/arrayUtil";
import {authentication} from "@/jurisdiction/authentication";
import {CHECK_STATE, LOGICAL} from "@/constantData";

/**
 * 操作类型编码
 */
const HANDEL_CODE_CONFIG = {
  // 编辑
  UPDATE_INS: 'updateIns',
  // 删除
  DELETE_INS: 'deleteIns',
  // 自查
  SELF_INSPECTION: 'selfInspection',
  // 隐患整改
  HIDDEN_DANGER_RECTIFICATION: 'hiddenDangerRectification',
  // 抽查
  SPOT_CHECK: 'spotCheck',
  // 督查
  SUPERVISE_AND_EXAMINE: 'superviseAndExamine',
  // 统计
  STATISTICS: 'statistics',
  // 检查详情
  CHECK_INFO: 'checkInfo',
  // 企业隐患复核
  QY_HIDDEN_DANGER_REVIEW: 'qyHiddenDangerReview',
  // 监管部门隐患初审
  JG_HIDDEN_DANGER_REVIEW: 'jgHiddenDangerReview',
  // 监管部门隐患复审
  JG_HIDDEN_DANGER_REVIEW_FS: 'jgHiddenDangerReviewFs',
  // 隐患查询
  HIDDEN_DANGER_QUERY: 'hiddenDangerQuery',
}

const HANDEL_NAME_CONFIG = {
  [HANDEL_CODE_CONFIG.UPDATE_INS]: '编辑',
  [HANDEL_CODE_CONFIG.DELETE_INS]: '删除',
  [HANDEL_CODE_CONFIG.SELF_INSPECTION]: '自查',
  [HANDEL_CODE_CONFIG.HIDDEN_DANGER_RECTIFICATION]: '隐患整改',
  [HANDEL_CODE_CONFIG.SPOT_CHECK]: '抽查企业',
  [HANDEL_CODE_CONFIG.SUPERVISE_AND_EXAMINE]: '督查企业',
  [HANDEL_CODE_CONFIG.STATISTICS]: '隐患统计',
  [HANDEL_CODE_CONFIG.CHECK_INFO]: '检查详情',
  // 企业
  [HANDEL_CODE_CONFIG.QY_HIDDEN_DANGER_REVIEW]: '隐患复审',
  // 监管部门 -- 隐患初审
  [HANDEL_CODE_CONFIG.JG_HIDDEN_DANGER_REVIEW]: '隐患初核',
  // 监管部门 -- 隐患复审
  [HANDEL_CODE_CONFIG.JG_HIDDEN_DANGER_REVIEW_FS]: '隐患复核',
  // 隐患查询
  [HANDEL_CODE_CONFIG.HIDDEN_DANGER_QUERY]: '隐患列表',
}

/**
 * 编辑
 * @type {{icon: string, label: string, typeCode: string}}
 */
const UPDATE_INS = {
  handelCode: HANDEL_CODE_CONFIG.UPDATE_INS,
  icon: 'el-icon-edit',
  type: 'primary',
  label: HANDEL_NAME_CONFIG[HANDEL_CODE_CONFIG.UPDATE_INS],
  funName: 'updateIns',
  param: {},
  // authority: {role: ['A:B:A'], logical: LOGICAL.AND},
  dataFilter: [
    {
      key: 'inspectState',
      value: [CHECK_STATE.DRAFT],
      logical: LOGICAL.OR,
    },
  ],
  dataFilterLogical: LOGICAL.OR,
}

/**
 * 删除
 * @type {{icon: string, label: string, typeCode: string}}
 */
const DELETE_INS = {
  handelCode: HANDEL_CODE_CONFIG.DELETE_INS,
  icon: 'el-icon-delete',
  type: 'primary',
  label: HANDEL_NAME_CONFIG[HANDEL_CODE_CONFIG.DELETE_INS],
  funName: 'deleteIns',
  param: {},
  // authority: {role: ['A:B:A'], logical: LOGICAL.AND},
  dataFilter: [
    {
      key: 'inspectState',
      value: [CHECK_STATE.DRAFT],
      logical: LOGICAL.OR,
    },
  ],
  dataFilterLogical: LOGICAL.OR,
}

/**
 * 自查
 * @type {{icon: string, label: string, typeCode: string}}
 */
const SELF_INSPECTION = {
  handelCode: HANDEL_CODE_CONFIG.SELF_INSPECTION,
  icon: 'el-icon-zoom-out',
  type: 'primary',
  label: HANDEL_NAME_CONFIG[HANDEL_CODE_CONFIG.SELF_INSPECTION],
  funName: 'goPage',
  param: {path: '/securityCheck/checkBusPage'},
  authority: {role: ['business:inspect_checker:a'], logical: LOGICAL.AND},
  dataFilter: [
    {
      key: 'inspectState',
      value: [CHECK_STATE.UNDERWAY],
      logical: LOGICAL.OR,
    },
  ],
  dataFilterLogical: LOGICAL.AND,
}

/**
 * 隐患整改
 * @type {{icon: string, label: string, typeCode: string}}
 */
const HIDDEN_DANGER_RECTIFICATION = {
  handelCode: HANDEL_CODE_CONFIG.HIDDEN_DANGER_RECTIFICATION,
  icon: 'el-icon-s-flag',
  type: 'primary',
  label: HANDEL_NAME_CONFIG[HANDEL_CODE_CONFIG.HIDDEN_DANGER_RECTIFICATION],
  funName: 'goPage',
  param: {path: '/securityCheck/checkBusPage'},
  authority: {role: ['business:inspect_safer:a'], logical: LOGICAL.AND},
  dataFilter: [
    {
      key: 'inspectState',
      value: [CHECK_STATE.UNDERWAY],
      logical: LOGICAL.OR,
    },
  ],
  dataFilterLogical: LOGICAL.AND,
}

/**
 * 抽查
 * @type {{icon: string, label: string, typeCode: string}}
 */
const SPOT_CHECK = {
  handelCode: HANDEL_CODE_CONFIG.SPOT_CHECK,
  icon: 'el-icon-bell',
  type: 'primary',
  label: HANDEL_NAME_CONFIG[HANDEL_CODE_CONFIG.SPOT_CHECK],
  funName: 'goPage',
  // param: {path: '/securityCheck/checkBusPage'},
  param: {path: '/securityCheck/checkBusPageInfo'},
  authority: {role: ['business:choucha:a'], logical: LOGICAL.AND},
  dataFilter: [
    {
      key: 'inspectState',
      value: [CHECK_STATE.UNDERWAY],
      logical: LOGICAL.OR,
    },
  ],
  dataFilterLogical: LOGICAL.AND,
}

/**
 * 督查
 * @type {{icon: string, label: string, typeCode: string}}
 */
const SUPERVISE_AND_EXAMINE = {
  handelCode: HANDEL_CODE_CONFIG.SUPERVISE_AND_EXAMINE,
  icon: 'el-icon-view',
  type: 'primary',
  label: HANDEL_NAME_CONFIG[HANDEL_CODE_CONFIG.SUPERVISE_AND_EXAMINE],
  funName: 'goPage',
  // param: {path: '/securityCheck/checkBusPage'},
  param: {path: '/securityCheck/checkBusPageInfo'},
  authority: {role: ['business:ducha:a'], logical: LOGICAL.AND},
  dataFilter: [
    {
      key: 'inspectState',
      value: [CHECK_STATE.UNDERWAY],
      logical: LOGICAL.OR,
    },
    {
      key: 'isCreated',
      value: ['1'],
      logical: LOGICAL.OR,
    },
  ],
  dataFilterLogical: LOGICAL.AND,
}

/**
 * 统计
 * @type {{icon: string, label: string, handelCode: string}}
 */
const STATISTICS = {
  handelCode: HANDEL_CODE_CONFIG.STATISTICS,
  icon: 'el-icon-s-data',
  label: HANDEL_NAME_CONFIG[HANDEL_CODE_CONFIG.STATISTICS],
  funName: 'goPage',
  param: {path: '/securityCheck/statistics'},
  authority: {role: ['business:inspect:tj'], logical: LOGICAL.AND},
  dataFilter: [
    {
      key: 'inspectState',
      value: [CHECK_STATE.UNDERWAY, CHECK_STATE.HAVE_BEEN_FILED],
      logical: LOGICAL.OR,
    },
  ],
  dataFilterLogical: LOGICAL.AND,
}

/**
 * 检查详情
 * @type {{param: {}, icon: string, label: string, handelCode: string, funName: string}}
 */
const CHECK_INFO = {
  handelCode: HANDEL_CODE_CONFIG.CHECK_INFO,
  icon: 'el-icon-info',
  label: HANDEL_NAME_CONFIG[HANDEL_CODE_CONFIG.CHECK_INFO],
  funName: 'showCurrentInfo',
  param: {},
  // authority: {role: ['A:B:D'], logical: LOGICAL.AND},
  dataFilter: [
    {
      key: 'inspectState',
      value: [CHECK_STATE.UNDERWAY, CHECK_STATE.HAVE_BEEN_FILED],
      logical: LOGICAL.OR,
    },
  ],
  dataFilterLogical: LOGICAL.OR,
}

/**
 * 企业隐患复核
 * @type {{param: {}, icon: string, label: string, handelCode: string, funName: string}}
 */
const QY_HIDDEN_DANGER_REVIEW = {
  handelCode: HANDEL_CODE_CONFIG.QY_HIDDEN_DANGER_REVIEW,
  icon: 'el-icon-s-check',
  type: 'primary',
  label: HANDEL_NAME_CONFIG[HANDEL_CODE_CONFIG.QY_HIDDEN_DANGER_REVIEW],
  funName: 'goPage',
  param: {path: '/securityCheck/checkBusPage'},
  authority: {role: ['business:inspect_e_leader:a'], logical: LOGICAL.AND},
  dataFilter: [
    {
      key: 'inspectState',
      value: [CHECK_STATE.UNDERWAY],
      logical: LOGICAL.OR,
    },
  ],
  dataFilterLogical: LOGICAL.AND,
}

/**
 * 监管部门隐患初审
 * @type {{param: {}, icon: string, label: string, handelCode: string, funName: string}}
 */
const JG_HIDDEN_DANGER_REVIEW = {
  handelCode: HANDEL_CODE_CONFIG.JG_HIDDEN_DANGER_REVIEW,
  icon: 'el-icon-s-check',
  type: 'primary',
  label: HANDEL_NAME_CONFIG[HANDEL_CODE_CONFIG.JG_HIDDEN_DANGER_REVIEW],
  funName: 'goPage',
  param: {path: '/securityCheck/checkBusPage'},
  authority: {role: ['business:inspect_sup:cs'], logical: LOGICAL.AND},
  dataFilter: [
    {
      key: 'inspectState',
      value: [CHECK_STATE.UNDERWAY],
      logical: LOGICAL.OR,
    },
  ],
  dataFilterLogical: LOGICAL.AND,
}

/**
 * 监管部门隐患复审
 * @type {{param: {}, icon: string, label: string, handelCode: string, funName: string}}
 */
const JG_HIDDEN_DANGER_REVIEW_FS = {
  handelCode: HANDEL_CODE_CONFIG.JG_HIDDEN_DANGER_REVIEW_FS,
  icon: 'el-icon-s-check',
  type: 'primary',
  label: HANDEL_NAME_CONFIG[HANDEL_CODE_CONFIG.JG_HIDDEN_DANGER_REVIEW_FS],
  funName: 'goPage',
  param: {path: '/securityCheck/checkBusPage'},
  authority: {role: ['business:inspect_sup:fs'], logical: LOGICAL.AND},
  dataFilter: [
    {
      key: 'inspectState',
      value: [CHECK_STATE.UNDERWAY],
      logical: LOGICAL.OR,
    },
  ],
  dataFilterLogical: LOGICAL.AND,
}

/**
 * 隐患列表
 * @type {{param: {}, icon: string, label: string, handelCode: string, funName: string}}
 */
const HIDDEN_DANGER_QUERY = {
  handelCode: HANDEL_CODE_CONFIG.HIDDEN_DANGER_QUERY,
  icon: 'el-icon-tickets',
  label: HANDEL_NAME_CONFIG[HANDEL_CODE_CONFIG.HIDDEN_DANGER_QUERY],
  funName: 'goPage',
  param: {path: '/securityCheck/checkBusPage'},
  authority: {role: ['business:inspect:tj'], logical: LOGICAL.AND},
  dataFilter: [
    {
      key: 'inspectState',
      value: [CHECK_STATE.UNDERWAY],
      logical: LOGICAL.OR,
    },
  ],
  dataFilterLogical: LOGICAL.AND,
}

const EVENT_CONFIG = [CHECK_INFO, HIDDEN_DANGER_QUERY, STATISTICS, UPDATE_INS, SELF_INSPECTION, HIDDEN_DANGER_RECTIFICATION, SPOT_CHECK, SUPERVISE_AND_EXAMINE, QY_HIDDEN_DANGER_REVIEW, JG_HIDDEN_DANGER_REVIEW, JG_HIDDEN_DANGER_REVIEW_FS, DELETE_INS]

const getCheckEventBtList = (data) => {
  const cBtItem = _.cloneDeep(EVENT_CONFIG).filter(item => {
    return item.authority ? authentication(item.authority) : true
  })
  if (!isArr(cBtItem)) return [];
  return _.cloneDeep(cBtItem).filter(item => {
    let {dataFilter, dataFilterLogical} = item
    if (!isArr(dataFilter) && (!dataFilterLogical || dataFilterLogical == '')) return true
    let dataFlagList = dataFilter.map(fItem => {
      let {key, value, logical} = fItem
      let vaFlagList = value.map(v => v == data[key])
      return logical == LOGICAL.OR ? vaFlagList.some(flag => flag) : vaFlagList.every(flag => flag)
    });
    return dataFilterLogical == LOGICAL.OR ? dataFlagList.some(flag => flag) : dataFlagList.every(flag => flag)
  })
}

export {HANDEL_NAME_CONFIG, getCheckEventBtList, EVENT_CONFIG, HANDEL_CODE_CONFIG}
